package ru.nsu.fit.client.core.soap.get_questions_stat;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import ru.nsu.fit.client.core.response.QuestionStat;
import ru.nsu.fit.client.core.response.QuestionStatData;
import ru.nsu.fit.client.core.soap.SOAPExecutor;

import java.util.*;

public class GetQuestionsStat {
    private static final String WSDL_NAME = "get_questions_stat";
    private static final String METHOD_NAME = "getQuestionsStat";
    private static final String NAMESPACE = "http://get_questions_stat.ws.fit.nsu.ru/";

    public static GetQuestionsStatOutput execute(final String serverLocation, long userSessionId, List<Integer> ids) throws Exception
    {
        String wsdlLocation = serverLocation + WSDL_NAME + "?wsdl";

        Set<Map.Entry<String, String>> params = new HashSet<Map.Entry<String,String>>();
        params.add(new AbstractMap.SimpleEntry<String, String>("userSessionId", String.valueOf(userSessionId)));
        for (Integer i: ids)
        {
            params.add(new AbstractMap.SimpleEntry<String, String>("questionIds", String.valueOf(i)));
        }

        SoapObject response = SOAPExecutor.executeSOAP(wsdlLocation, NAMESPACE, METHOD_NAME, params);

        GetQuestionsStatOutput out = new GetQuestionsStatOutput();
        if (response.hasProperty("errorMessage"))
        {
            out.setErrorMessage(response.getPropertyAsString("errorMessage"));
        }
        if (response.hasProperty("errorCode"))
        {
            out.setErrorCode(Integer.parseInt(response.getPropertyAsString("errorCode")));
        }

        PropertyInfo info = new PropertyInfo();
        ArrayList<Integer> questionIds = new ArrayList<Integer>();
        ArrayList<Integer> lifetimes = new ArrayList<Integer>();
        ArrayList<Integer> incomes = new ArrayList<Integer>();
        ArrayList<Boolean> isMines = new ArrayList<Boolean>();
        ArrayList<String> questionTexts = new ArrayList<String>();
        ArrayList<ArrayList<Integer>> answerIds = new ArrayList<ArrayList<Integer>>();
        ArrayList<ArrayList<String>> answers = new ArrayList<ArrayList<String>>();
        ArrayList<ArrayList<Integer>> answerHits = new ArrayList<ArrayList<Integer>>();

        for (int i = 0; i < response.getPropertyCount(); ++i)
        {
            response.getPropertyInfo(i, info);
            if (info.getName().equals("questionIds"))
            {
                questionIds.add(Integer.parseInt(info.getValue().toString()));
            }
            else if (info.getName().equals("lifetimes"))
            {
                lifetimes.add(Integer.parseInt(info.getValue().toString()));
            }
            else if (info.getName().equals("incomes"))
            {
                incomes.add(Integer.parseInt(info.getValue().toString()));
            }
            else if (info.getName().equals("doesQuestionBelongToMe"))
            {
                isMines.add(Boolean.parseBoolean(info.getValue().toString()));
            }
            else if (info.getName().equals("questionTexts"))
            {
                questionTexts.add(info.getValue().toString());
            }
            else if (info.getName().equals("answers"))
            {
                SoapObject prop = (SoapObject)info.getValue();
                ArrayList<String> array = new ArrayList<String>();
                for (int j = 0; j < prop.getPropertyCount(); ++j)
                {
                    array.add(prop.getPropertyAsString(j));
                }
                answers.add(array);
            }
            else if (info.getName().equals("answerIds"))
            {
                SoapObject prop = (SoapObject)info.getValue();
                ArrayList<Integer> array = new ArrayList<Integer>();
                for (int j = 0; j < prop.getPropertyCount(); ++j)
                {
                    array.add(Integer.parseInt(prop.getPropertyAsString(j)));
                }
                answerIds.add(array);
            }
            else if (info.getName().equals("answerHits"))
            {
                SoapObject prop = (SoapObject)info.getValue();
                ArrayList<Integer> array = new ArrayList<Integer>();
                for (int j = 0; j < prop.getPropertyCount(); ++j)
                {
                    array.add(Integer.parseInt(prop.getPropertyAsString(j)));
                }
                answerHits.add(array);
            }
        }

        QuestionStatData data = new QuestionStatData();
        for (int i = 0; i < questionIds.size(); ++i)
        {
            QuestionStat question = new QuestionStat(questionIds.get(i), questionTexts.get(i), lifetimes.get(i));
            question.setAnswerIds(answerIds.get(i));
            question.setAnswers(answers.get(i));
            question.setAnswerHits(answerHits.get(i));
            question.setIncome(incomes.get(i));
            question.setMine(isMines.get(i));
            data.getQuestions().add(question);
        }

        out.setData(data);

        return out;
    }
}
